# -*- coding: utf-8 -*-
def commit (values):
    query = r" INSERT INTO tbl_account (event_id, amount, date, flag_value, description, url, active) VALUES ('%s', '%s', '%s', %s, '%s', '%s', True)"
    print query %(values[0], values[1], values[2], values[3], values[4], values[5])
    return query %(values[0], values[1], values[2], values[3], values[4], values[5])

def update_actions (name, parent_id = None):
    # to be executed all the time one makes changes to db
    if parent_id == None:
        query = r"INSERT INTO tbl_actions (author_name) VALUES ('%s');"
        print query % name
        return query % name
    else:
        query = r"INSERT INTO tbl_actions (author_name, parent_eventid) VALUES ('%s', '%s')"
        print query % (name, parent_id)
        return query % (name, parent_id)

def extract_from_actions (name):
    query = r"SELECT ac.event_id FROM tbl_actions ac WHERE ac.author_name = '%s' ORDER BY ac.creation_datetime DESC LIMIT 1;"
    return query % name

def dashboard_account ():
    query = r"SELECT acc.amount, acc.flag_value FROM tbl_account acc WHERE acc.active = True AND date >= FIRST_DAY(CURDATE()) AND date <= CURDATE();"
    return query

def dashboard_commits():
    query = r"SELECT ac.event_id AS event, author_name, date, description, flag_value, amount, url FROM tbl_account acc INNER JOIN tbl_actions ac ON acc.event_id = ac.event_id WHERE active = True AND date >= FIRST_DAY(CURDATE()) AND date <= CURDATE() ORDER BY date ASC;"
    ##print query
    return query

def disable_commit (event_id):
    query = r"UPDATE tbl_account SET active = False WHERE event_id ='%s'"
    #print query % event_id
    return query % event_id

def add_idea (data):
    query = """
    INSERT INTO tbl_planning
    (event_id, price_low, price_high, start_date, finish_date, description, state)
    VALUES ('%s', '%s', '%s', '%s', '%s', '%s', 1)
    """
    #print query % (data[0], data[1], data[2], data[3], data[4], data[5])
    return query % (data[0], data[1], data[2], data[3], data[4], data[5])

def find_planning_data(dates):
    query = """
    SELECT
        ac.event_id AS event,
        author_name,
        price_low,
        price_high,
        start_date,
        finish_date,
        description
    FROM
        tbl_actions ac
        INNER JOIN tbl_planning pl
        ON pl.event_id = ac.event_id
    WHERE
        state = '1' AND
        start_date >= '%s' AND
        finish_date <= '%s'
    """
    #print query % (dates[0], dates[1])
    return query % (dates[0], dates[1])

def remove_plan(parent_id):
    query = """
    UPDATE
        tbl_planning
    SET
        state = 0
    WHERE
        event_id = '%s'
    """
    #print query % parent_id
    return query % parent_id


def planning_state_change(state_id, parent_id):
    query = """
    UPDATE
        tbl_planning
    SET
        state = %s
    WHERE
        event_id = %s
    """
    #print query % (state_id, parent_id)
    return query % (state_id, parent_id)




